package cn.ym.mall.service.impl;

import cn.ym.mall.dao.OrderDao;
import cn.ym.mall.dao.impl.OrderDaoImpl;
import cn.ym.mall.entity.Order;
import cn.ym.mall.service.OrderService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();

    /**
     * 新增订单
     * @param order 添加的订单
     * @return
     * @throws SQLException
     */
    @Override
    public boolean addOrder(Order order) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
            if (orderDao.addOrder(order) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据订单id删除订单
     *
     * @param oid 订单
     * @return
     */
    @Override
    public boolean deleteByOid(Integer oid) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
            if (orderDao.deleteByOid(oid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 通过订单id查询订单信息
     *
     * @param oid
     * @return
     */
    @Override
    public Order getOrderByOid(Integer oid) throws SQLException {
        Order order = null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            order = orderDao.getOrderByOid(oid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return order;
    }

    /**
     * 通过商家id和订单状态查询订单信息
     *
     * @param sid
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> getSumBySid(Integer sid) throws SQLException {
        List<Order> orderSidList = new ArrayList<>();
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            orderSidList = orderDao.getSumBySid(sid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return orderSidList;
    }

    /**
     * 分页查询订单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Order> getPageOrder(Integer pageNo, Integer pageSize) throws SQLException {
        Connection connection=null;
        Page<Order> page = new Page<Order>();
        try {
            connection = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)orderDao.getOrderCount());
            page.setList(orderDao.getPageOrder(pageNo,pageSize));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过商店编号查询订单（分页）
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Order> getPageOrderBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection connection = null;
        Page<Order> page = new Page<Order>();
        try {
            connection = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)orderDao.getCountBySid(sid));
            page.setList(orderDao.getPageOrderBySid(sid,pageNo,pageSize));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }


    /**
     * 通过用户id看订单(分页)
     *
     * @param uid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Order> getPageOrderByuid(Integer uid, Integer pageNo, Integer pageSize) throws SQLException {   Connection connection = null;
        Page<Order> page = new Page<Order>();
        try {
            connection = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)orderDao.getCountBySid(uid));
            page.setList(orderDao.getPageOrderBySid(uid,pageNo,pageSize));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

}
